﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using CommonLib;

namespace YouErYuanManage.Filter
{
    /// <summary>
    /// 验证用户登陆授权
    /// </summary>
    public sealed class IsLoginAttribute : FilterAttribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            object user = filterContext.HttpContext.Session["user"];
            bool isAjax = filterContext.HttpContext.Request.IsAjaxRequest();
            if (isAjax)
            {
                if (user == null)
                {                    
                    string json = CommonMethod.FormatJsonObj("700", "登录状态已失效"); 
                    ContentResult content = new ContentResult();
                    content.Content = json;
                    filterContext.Result = content;
                }
            }
            else
            {
                if (user == null)
                {
                    ActionResult result = new RedirectResult("/Login/LoginView");
                    filterContext.Result = result;
                }
            }
        }
    }
}
